<?php
// 测试更新API
require_once '../utils/Database.php';
require_once '../utils/Response.php';

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    exit(0);
}

try {
    $db = new Database();
    
    // 获取第一个用户进行测试
    $user = $db->fetchOne("SELECT * FROM users LIMIT 1");
    
    if (!$user) {
        Response::error('没有找到测试用户');
    }
    
    $testData = [
        'nickname' => 'Test Nickname ' . date('H:i:s'),
        'phone' => '13800138000',
        'real_name' => 'Test Real Name',
        'updated_at' => date('Y-m-d H:i:s')
    ];
    
    echo json_encode([
        'message' => '开始测试更新',
        'user_id' => $user['id'],
        'test_data' => $testData
    ]);
    
    $result = $db->update('users', $testData, 'id = :user_id', ['user_id' => $user['id']]);
    
    $updatedUser = $db->fetchOne("SELECT * FROM users WHERE id = ?", [$user['id']]);
    
    Response::success([
        'update_result' => $result,
        'original_user' => $user,
        'updated_user' => $updatedUser
    ], '测试完成');
    
} catch (Exception $e) {
    Response::error('测试失败: ' . $e->getMessage());
}
?>